package com.tebiecloud.yunst.server.utils;

import java.util.HashMap;
import java.util.Map;

public class UrlUtils {

    public static class UrlEntity {
        /**
         * 基础url
         */
        public String baseUrl;
        /**
         * url参数
         */
        public Map<String, String> params;
    }

    /**
     * 解析url
     *
     * @param url
     * @return
     */
    public static UrlEntity parse(String url) {
        UrlEntity entity = new UrlEntity();
        if (url == null) {
            return entity;
        }
        url = url.trim();
        if (url.equals("")) {
            return entity;
        }
        String[] urlParts = url.split("\\?");
        entity.baseUrl = urlParts[0];
        //没有参数
        if (urlParts.length == 1) {
            return entity;
        }
        //有参数
        String[] params = urlParts[1].split("&");
        entity.params = new HashMap<>();
        for (String param : params) {
            String[] keyValue = param.split("=");
            entity.params.put(keyValue[0], keyValue[1]);
        }

        return entity;
    }

    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) {
        UrlEntity entity = parse("http://116.228.64.55:6900/yungateway/member/signContract.html?sysid=1902271423530473681&v=2.0&timestamp=2019-10-10+10%3A49%3A31&sign=YfeEiY5YBoeY1uLxsJx2yx3yXhIqJRK4gMY5mS4bZ252aQJAsh4ZfBDI77qweWPY9QVEE8ilOm%2FRhlfAQPgUxsENWLj%2B2C3nCXOI2UAwuBR33Mat3jaMIOhij4DD%2BCATwpE2rdMFYwRZgqo9OkxsNP5jgvVfk6llC5msI96CjoQ%3D&req=%7B%22method%22%3A%22signContract%22%2C%22param%22%3A%7B%22bizUserId%22%3A%22B39227D4C0A80CC11FCB6B40D8299179%22%2C%22memberType%22%3A%223%22%2C%22jumpUrl%22%3A%22https%3A%2F%2Ftebie.gzqqs.com%2Fyunstuser%2Fsigncontract%2Fupdate%22%2C%22backUrl%22%3A%22https%3A%2F%2Ftebie.gzqqs.com%2Fyunstuser%2Fsigncontract%2Fupdate%22%2C%22source%22%3A%222%22%7D%2C%22service%22%3A%22MemberService%22%7D&");
        System.out.println(entity.params.get("sysid"));
    }
}
